package cn.fl.ref.fncprjprdpaysch.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DecimalUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.constant.PrjQuatPrpsConstant;
import cn.fl.ref.fncprjprdpaysch.service.IFncPrjPrdPaySchMService;
import cn.fl.ref.fncprjprdpaysch.transform.FncPrjPrdPaySchDVOTransform;
import cn.fl.ref.fncprjprdpaysch.transform.FncPrjPrdPaySchMVOTransform;
import cn.fl.ref.fncprjprdpaysch.vo.FncPrjPrdPaySchDVO;
import cn.fl.ref.fncprjprdpaysch.vo.FncPrjPrdPaySchMVO;
import fl.ref.fncprjprdpaysch.condition.FncPrjPrdPaySchDQueryCondition;
import fl.ref.fncprjprdpaysch.condition.FncPrjPrdPaySchMQueryCondition;
import fl.ref.fncprjprdpaysch.dto.FncPrjPrdPaySchDDTO;
import fl.ref.fncprjprdpaysch.service.IFncPrjPrdPaySchDApiService;
import fl.ref.fncprjprdpaysch.service.IFncPrjPrdPaySchMApiService;

@Service
public class FncPrjPrdPaySchMServiceImpl implements IFncPrjPrdPaySchMService {
	@Autowired
	private IFncPrjPrdPaySchMApiService fncPrjPrdPaySchMApiService;
	@Autowired
	private IFncPrjPrdPaySchDApiService fncPrjPrdPaySchDApiService;
	@Override
	public Long add(FncPrjPrdPaySchMVO entity) throws BaseException {
		// TODO Auto-generated method stub
		return fncPrjPrdPaySchMApiService.add(FncPrjPrdPaySchMVOTransform.toDTO(entity));
	}

	@Override
	public boolean update(FncPrjPrdPaySchMVO entity) throws BaseException {
		// TODO Auto-generated method stub
		return fncPrjPrdPaySchMApiService.update(FncPrjPrdPaySchMVOTransform.toDTO(entity));
	}

	@Override
	public FncPrjPrdPaySchMVO selectById(Long id) throws BaseException {
		// TODO Auto-generated method stub
		return FncPrjPrdPaySchMVOTransform.toVO(fncPrjPrdPaySchMApiService.selectById(id));
	}

	@Override
	public List<FncPrjPrdPaySchMVO> searchList(FncPrjPrdPaySchMQueryCondition condition) throws BaseException {
		// TODO Auto-generated method stub
		condition.setSysCd(CurrentThreadContext.getCurrentSysCd());
		condition.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		return FncPrjPrdPaySchMVOTransform.toVOList(fncPrjPrdPaySchMApiService.searchList(condition));
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		// TODO Auto-generated method stub
		return fncPrjPrdPaySchMApiService.removeById(id);
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		// TODO Auto-generated method stub
		return fncPrjPrdPaySchMApiService.searchListPage(page);
	}

	@Override
	public Pager searchListPage(Pager page, String sysCd, String orgCd) throws BaseException {
		// TODO Auto-generated method stub
		return fncPrjPrdPaySchMApiService.searchListPage(page, sysCd, orgCd);
	}

	@Override
	public Pager selectFncPrjPrdPaySchMMgrListPage(Pager page) throws BaseException {
		return this.fncPrjPrdPaySchMApiService.selectFncPrjPrdPaySchMMgrListPage(page);
	}

	@Override
	public FncPrjPrdPaySchMVO selectPrjPrdPaySchMAndDetailById(Long id) throws BaseException {
		// TODO Auto-generated method stub
		FncPrjPrdPaySchMVO prjPrdPaySchM = null;
	        if (StringUtils.isNotEmpty(id)) {
	            //项目产品支付表主信息表：prj_prd_pay_sch_m
	            prjPrdPaySchM = this.selectById(id);
	            //支付明细
	            List<FncPrjPrdPaySchDDTO> payList = new ArrayList<FncPrjPrdPaySchDDTO>();
	            //费用明细
	            List<FncPrjPrdPaySchDDTO> feeList = new ArrayList<FncPrjPrdPaySchDDTO>();
	            List<FncPrjPrdPaySchDDTO> list = null;
	            if (null != prjPrdPaySchM) {
	            	FncPrjPrdPaySchDQueryCondition qc = new FncPrjPrdPaySchDQueryCondition();
	                qc.setPaySchId(id);
	                //这两个条件目前没用到
	                qc.setSortName("FEE_TYP_CD");
	                qc.setSortOrder("ASC");
	                qc.setSysCd(CurrentThreadContext.getCurrentSysCd());
	                qc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
	                qc.setIsDel(prjPrdPaySchM.getIsDel());
	                //查询十张表
	                list = this.fncPrjPrdPaySchDApiService.searchList(qc);
	                if (CollectionUtils.isNotEmpty(list)) {
	                	long totalRntAmt=0L,firstPayAmt=0L;
	                    for (FncPrjPrdPaySchDDTO prjPrdPaySchDDTO : list) {
	                        //费用类型  租金、利息、本金、剩余本金
	                        if (prjPrdPaySchDDTO.getFeeTypCd().equals(PrjQuatPrpsConstant.CODE_FEE_WAY_TYPE_RNT_PRICE)
	                                || prjPrdPaySchDDTO.getFeeTypCd().equals(PrjQuatPrpsConstant.CODE_FEE_WAY_TYPE_INT_PRICE)
	                                || prjPrdPaySchDDTO.getFeeTypCd().equals(PrjQuatPrpsConstant.CODE_FEE_WAY_TYPE_PRINT_PRICE)
	                                || prjPrdPaySchDDTO.getFeeTypCd().equals(PrjQuatPrpsConstant.CODE_FEE_WAY_TYPE_REMPRIN_PRICE)) {
	                            //支付明细
	                            payList.add(prjPrdPaySchDDTO);
	                            if (prjPrdPaySchDDTO.getFeeTypCd().equals(PrjQuatPrpsConstant.CODE_FEE_WAY_TYPE_RNT_PRICE)) {
	                            	totalRntAmt=totalRntAmt+(prjPrdPaySchDDTO.getFeeAmt()==null ? 0 : prjPrdPaySchDDTO.getFeeAmt().longValue());
								}
	                        } else {
	                            //期次为0：费用      支付时间                                                       租赁期数
	                            if (prjPrdPaySchDDTO.getPayPd() == 0 || prjPrdPaySchDDTO.getPayPd() == prjPrdPaySchM.getLsPd()) {
	                                //费用明细
	                                feeList.add(prjPrdPaySchDDTO);
	                            }
	                            firstPayAmt=firstPayAmt+  (prjPrdPaySchDDTO.getFeeAmt()== null ? 0 : prjPrdPaySchDDTO.getFeeAmt().longValue());
								
	                        }
	                    }
	                    prjPrdPaySchM.setTotalRntAmt(DecimalUtils.convertFenToYuan(totalRntAmt));
	                    prjPrdPaySchM.setFirstPayAmt(DecimalUtils.convertFenToYuan(firstPayAmt));
	                    prjPrdPaySchM.setFeeDetailList(FncPrjPrdPaySchDVOTransform.toVOList(feeList));
	                    prjPrdPaySchM.setPrjPrdPaySchDList(FncPrjPrdPaySchDVOTransform.toVOList(payList));
	                    //支付时间排序（没必要放在循环里面）
	                    this.sortPrjPrdPayList(prjPrdPaySchM.getPrjPrdPaySchDList());
	                }

	                //setPrjPrdQuatPrpsFeeDictOption(list, sysCd, orgCd);
//	                List<FncPrjPrdPaySchMDTO> dicList = new ArrayList<FncPrjPrdPaySchMDTO>();
//	                dicList.add(prjPrdPaySchM);
//	                setDictionaryOption(dicList, sysCd, orgCd);
//	                //查询项目产品明细信息
//	                PrjPrdDtlQueryCondition dtlQc = new PrjPrdDtlQueryCondition();
//	                dtlQc.setPaySchId(prjPrdPaySchM.getId());
//	                //项目产品明细信息表：prj_prd_dtl
//	                List<PrjPrdDtlDTO> prjPrdDtlList = this.prjPrdDtlService.searchList(dtlQc);
//	                if (CollectionUtils.isNotEmpty(prjPrdDtlList)) {
//	                    PrjPrdDtlDTO dtlDTO = prjPrdDtlList.get(0);
//	                    PrjPrdBscInfoQueryCondition prjPrdQc = new PrjPrdBscInfoQueryCondition();
//	                    List<Long> idList = new ArrayList<Long>();
//	                    idList.add(dtlDTO.getPrjPrdId());
//	                    prjPrdQc.setIdList(idList);
//	                    //项目产品基础信息表：prj_prd_bsc_info    (没必要查list)
//	                    List<PrjPrdBscInfoDTO> prjPrdBscInfoList = this.prjPrdBscInfoService.searchList(prjPrdQc, sysCd, orgCd);
//	                    if (CollectionUtils.isNotEmpty(prjPrdBscInfoList)) {
//	                        prjPrdPaySchM.setPrjPrdBsc(prjPrdBscInfoList.get(0));
//	                    }
//	                    prjPrdPaySchM.setDtl(dtlDTO);
//	                }
	            }
	        }
	        return prjPrdPaySchM;
	}
	public List<FncPrjPrdPaySchDVO> sortPrjPrdPayList(List<FncPrjPrdPaySchDVO> prjPrdPayList) {
        Collections.sort(prjPrdPayList, new Comparator<FncPrjPrdPaySchDVO>() {
            public int compare(FncPrjPrdPaySchDVO o1, FncPrjPrdPaySchDVO o2) {
                if (o1.getPayPd().intValue() > o2.getPayPd().intValue()) {
                    return 1;
                }
                if (o1.getPayPd().intValue() == o2.getPayPd().intValue()) {
                    return 0;
                }
                return -1;
            }
        });
        return prjPrdPayList;
    }

}
